package handler

import (
	"ecom-manage/cache"
	"ecom-manage/model"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func GoodAdd(c *gin.Context) {
	good_title := c.PostForm("good_title")
	good_status := c.PostForm("good_status")
	good_price := c.PostForm("good_price")
	good_num := c.PostForm("good_num")

	data := model.Good{
		GoodTitle:  good_title,
		GoodStatus: good_status,
		GoodPrice:  good_price,
		GoodNum:    good_num,
	}
	model.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
	})

}
func OrderAdd(c *gin.Context) {
	order_code := c.PostForm("order_code")
	user_comment := c.PostForm("user_comment")
	order_good := c.PostForm("order_good")
	order_price := c.PostForm("order_price")
	zf_type := c.PostForm("zf_type")
	data := model.GoodOrder{
		OrderCode:   order_code,
		UserComment: user_comment,
		OrderGood:   order_good,
		OrderPrice:  order_price,
		ZfType:      zf_type,
	}
	model.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
	})
}

func OrderList(c *gin.Context) {
	/*
		SELECT
		good_orders.gid as Gid,
		good_orders.order_code as OrderCode,
		good_orders.user_comment as UserComment,
		good_orders.order_good as OrderGood,
		good_orders.order_price as OrderPrice,
		good_orders.zf_type as ZfType,
		goods.good_title as GoodTitle,
		goods.good_status as GoodStatus,
		goods.good_price as GoodPrice,
		goods.good_num as GoodNum,
		FROM
		good_orders
		LEFT JOIN goods on goods.id=good_orders.gid
	*/

	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "5"))
	offset := (page - 1) * size
	var orderList []model.GoodOrderList
	redis_key := "order_list" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("good_orders").Select(
			"good_orders.gid as Gid",
			"good_orders.order_code as OrderCode",
			"good_orders.user_comment as UserComment",
			"good_orders.order_good as OrderGood",
			"good_orders.order_price as OrderPrice",
			"good_orders.zf_type as ZfType",
			"goods.good_title as GoodTitle",
			"goods.good_status as GoodStatus",
			"goods.good_price as GoodPrice",
			"goods.good_num as GoodNum",
		).Joins("LEFT JOIN goods on goods.id=good_orders.gid").
			Offset(offset).Limit(size).Debug().Find(&orderList)
		jsonData, _ := json.Marshal(orderList)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &orderList)
	}
	var total int64
	model.DB.Model(&model.GoodOrder{}).Count(&total)

	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "成功",
		"data":  orderList,
		"total": total,
	})

}

func OrderDetail(c *gin.Context) {
	id := c.Query("id")

	var orderList model.GoodOrderList
	redis_key := "order_list" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("good_orders").Select(
			"good_orders.gid as Gid",
			"good_orders.order_code as OrderCode",
			"good_orders.user_comment as UserComment",
			"good_orders.order_good as OrderGood",
			"good_orders.order_price as OrderPrice",
			"good_orders.zf_type as ZfType",
			"goods.good_title as GoodTitle",
			"goods.good_status as GoodStatus",
			"goods.good_price as GoodPrice",
			"goods.good_num as GoodNum",
		).Joins("LEFT JOIN goods on goods.id=good_orders.gid").
			Where("good_orders.id=?", id).Debug().Find(&orderList)
		jsonData, _ := json.Marshal(orderList)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &orderList)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "成功",
		"data": orderList,
	})
}
